package com.mustafaiev.tair.mweather;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.mustafaiev.tair.mweather.database.MWeatherDBHelper;

public class MWeatherContentProvider extends ContentProvider {

	MWeatherDBHelper dbHelper;

	public final static String AUTHORITY = "com.mustafaiev.tair.mweather.mweathercontentprovider";
	public final static String PATH = "/towns";
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ MWeatherContentProvider.AUTHORITY + MWeatherContentProvider.PATH);

	private static final int SEARCH_WORDS = 0;
	private static final int SEARCH_SINGLE_WORD = 1;
	private static final int SEARCH_SUGGEST_QUERY = 3;

	private final UriMatcher uriMatcher = this.buildUriMather();

	private UriMatcher buildUriMather() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

		// if search all words
		matcher.addURI(MWeatherContentProvider.AUTHORITY,
				MWeatherContentProvider.PATH,
				MWeatherContentProvider.SEARCH_WORDS);
		// if search single word
		matcher.addURI(MWeatherContentProvider.AUTHORITY,
				MWeatherContentProvider.PATH + "/#",
				MWeatherContentProvider.SEARCH_SINGLE_WORD);
		// if search suggest query
		matcher.addURI(MWeatherContentProvider.AUTHORITY,
				SearchManager.SUGGEST_URI_PATH_QUERY,
				MWeatherContentProvider.SEARCH_SUGGEST_QUERY);
		matcher.addURI(MWeatherContentProvider.AUTHORITY,
				SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
				MWeatherContentProvider.SEARCH_SUGGEST_QUERY);

		return matcher;

	}

	@Override
	public String getType(Uri uri) {

		return null;
	}

	@Override
	public boolean onCreate() {
		this.dbHelper = new MWeatherDBHelper(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		switch (this.uriMatcher.match(uri)) {
		case SEARCH_WORDS:
			return this.getWord(uri);
		case SEARCH_SUGGEST_QUERY:
			return this.getSuggestions(selectionArgs[0]);

		}

		return null;
	}

	/**
	 * Creation date: 4 Sep 2011 02:02:33
	 * 
	 * @param query
	 * @return {@link Cursor} result
	 */
	private Cursor getSuggestions(String query) {
		query = query.toLowerCase();
		String[] columns = new String[] { BaseColumns._ID,
				MWeatherDBHelper.MWEATHER_COLUMN_TOWN_NAME,
				SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID };

		return this.dbHelper.getWordMatches(query, columns);
	}

	private Cursor getWord(Uri uri) {
		String rowId = uri.getLastPathSegment();
		String[] columns = new String[] { BaseColumns._ID,
				MWeatherDBHelper.MWEATHER_COLUMN_TOWN_NAME,
				SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID };

		return this.dbHelper.getSingleWord(rowId, columns);
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}
}
